package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.utils.StringUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.Test;
import com.ruoyi.system.service.ITestService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * testController
 * 
 * @author ruoyi
 * @date 2020-09-17
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/test" )
public class TestMpController extends BaseController {

    private final ITestService iTestService;

    /**
     * 查询test列表
     */
    @PreAuthorize("@ss.hasPermi('system:test:list')")
    @GetMapping("/list")
    public TableDataInfo list(Test test)
    {
        startPage();
        LambdaQueryWrapper<Test> lqw = new LambdaQueryWrapper<Test>();
        if (StringUtils.isNotBlank(test.getName())){
            lqw.like(Test::getName ,test.getName());
        }
        if (test.getGmtTime() != null){
            lqw.eq(Test::getGmtTime ,test.getGmtTime());
        }
        if (StringUtils.isNotBlank(test.getCreator())){
            lqw.eq(Test::getCreator ,test.getCreator());
        }
        List<Test> list = iTestService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 导出test列表
     */
    @PreAuthorize("@ss.hasPermi('system:test:export')" )
    @Log(title = "test" , businessType = BusinessType.EXPORT)
    @GetMapping("/export" )
    public AjaxResult export(Test test) {
        LambdaQueryWrapper<Test> lqw = new LambdaQueryWrapper<Test>(test);
        List<Test> list = iTestService.list(lqw);
        ExcelUtil<Test> util = new ExcelUtil<Test>(Test. class);
        return util.exportExcel(list, "test" );
    }

    /**
     * 获取test详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:test:query')" )
    @GetMapping(value = "/{testId}" )
    public AjaxResult getInfo(@PathVariable("testId" ) Long testId) {
        return AjaxResult.success(iTestService.getById(testId));
    }

    /**
     * 新增test
     */
    @PreAuthorize("@ss.hasPermi('system:test:add')" )
    @Log(title = "test" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Test test) {
        return toAjax(iTestService.save(test) ? 1 : 0);
    }

    /**
     * 修改test
     */
    @PreAuthorize("@ss.hasPermi('system:test:edit')" )
    @Log(title = "test" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Test test) {
        return toAjax(iTestService.updateById(test) ? 1 : 0);
    }

    /**
     * 删除test
     */
    @PreAuthorize("@ss.hasPermi('system:test:remove')" )
    @Log(title = "test" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{testIds}" )
    public AjaxResult remove(@PathVariable Long[] testIds) {
        return toAjax(iTestService.removeByIds(Arrays.asList(testIds)) ? 1 : 0);
    }
}
